$(document).ready(function () {
    var platform = 'qiancheng';
    var industry = '证券';
    var pageNum = 1;
    var count = 50;

    function jobs_filter(init){
        $('#jobs-container tbody').html('');
        $('#jobs-container thead').hide();
        $('#loading').show();

        $.ajax({
            'url':'/jobs_filter',
            'dataType': 'json',
            'data':{
                'start' : (pageNum - 1) * count,
                'industry':industry,
                'platform':platform,
                'count' : count
            },
            'success':function(resp){
                if(resp.success){
                    var data = resp.data;
                    var table_doc = '';

                    var platform_name = {
                        'qiancheng':'前程无忧',
                        'zhilian':'智联招聘',
                        'dajie':'大街网',
                        'yingcai':'中华英才',
                        'lagou':'拉勾网',
                        'offical':'官网'
                    }

                    var divide_name  = {
                        'f':'前台',
                        'm':'中台',
                        'b':'后台',
                        'fm':'中后台'
                    }

                    for (i = 0;i < data.length;i++){
                            var item = resp.data[i];

                            table_doc += '<tr origin = "' + item.from + '">' +
                                '<th scope = "row" style="text-align:center;">' + (i + 1) + '</th>' +
                                '<td><a class = "job-name">' + item.name + '</a></td>' +
                                '<td class = "tc">' + item.searchKeyword + '</td>' +
                                '<td class = "tc">' + (item.salary2 ? item.salary2 : item.salary.join('-')) + '</td>' +
                                '<td class = "tc">' + (item.divide ? divide_name[item.divide] : '-') + '</td>' +
                                '<td class = "tc">' + item.company_industry + '</td>' +
                                '<td class = "tc">' + item.location + '</td>' +
                                '<td class = "tc" title = "">' + (item.exp.length ? '' : '是') + '</td>' +
                                '<td class = "tc">' + item.edu + '</td>' +
                                '<td class = "tc">' + item.count + '</td>' +
                                '<td class = "tc">' + item.date + '</td>' +
                                '<td class = "tc"><a class = "comp-name" target="_blank" href = "'+item.company_url+'">' + item.company + '</a></td>' +
                                '<td class = "tc">' + item.company_type + '</td>' +
                                '<td class = "tc">' + platform_name[item.platform] + '</td>' +
//                                '<td class = "tc">' + '操作' + '</td>' +
                                '</tr><tr class = "description hide">'+
                                '<td class = "" colspan = "14">' + item.description + '</td>' +
                                '</tr>';
                    }

                    $('#jobs-container tbody').html(table_doc);
                    $('#jobs-container thead').show();
                    $('#loading').hide();

                    $('.job-name').click(function(e){
                        $(e.target).closest('tr').next().toggleClass('hide').siblings('.description').addClass('hide');
                    });

                    $('#download').attr('href','/download_jobs?platform=' + platform);
                    $("#page-container-static-normal").page({
                         count : resp.total,
                         pageSize:50,
                         pageNum:pageNum,
                         theme:"normal"
                    });

                    if(init){
                        console.log('bind')
                         $("#page-container-static-normal").on("pageChanged",function (event,params) {
                               $(this).data("page").refresh(params);
                               pageNum = Number(params.pageNum);
                               jobs_filter();
                         });
                    }

                    if(platform == 'offical'){
                        $('#industry_available .label').css('opacity',0.3);
                    }else{
                        $('#industry_available .label').css('opacity',1);
                    }
                }
            }
        })
    }

    jobs_filter(true);

    $('#industry_available .label').click(function (e) {
            if(platform == 'offical'){
                return;
            }
            $(this).siblings().removeClass('label-primary');
            $(this).addClass('label-primary');

            industry = $(e.target).attr('data');
            pageNum = 1;
            jobs_filter();
    });

    $('#platform_available .label').click(function (e) {
            $(this).siblings().removeClass('label-primary');
            $(this).addClass('label-primary');

            platform = $(e.target).attr('data');
            pageNum = 1;
            jobs_filter();
    });

    $.ajax({
        'url':'/job_count',
        'dataType': 'json',
        'success':function(resp){
            if(resp.success){
                data = resp.data;
                for (i in data){
                    $('#'+i).text($('#'+i).text() + '(' + (data[i].posts / 10000).toFixed(2) + 'w)');
                }
            }
        }
    });
});